package com.lyy.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.lyy.Vo.UserVo;
import com.lyy.Vo.UserVoPage;
import com.lyy.entity.User;
import com.lyy.entity.param.UserInsertParam;
import com.lyy.entity.param.UserUpdateParam;
import com.lyy.result.Result;
import com.lyy.service.UserService;
import lombok.Data;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Data
@ResponseBody
@RequestMapping("/api/v1/user")
public class UserController {
    public UserService userService;
    @RequestMapping("/select-by-user-id/{user-id}")
    public Result selectByOrderId(@PathVariable("user-id") Integer userId){
        User user=userService.selectByUserId(userId);
        if (ObjectUtil.isEmpty(user)) {
            return Result.failure();
        }
        UserVo userVo = new UserVo();
        BeanUtil.copyProperties(user, userVo);
        return Result.success(userVo);
    }

    @RequestMapping("/insert")
    public Result insert(@RequestBody UserInsertParam userInsertParam){
        User user=BeanUtil.copyProperties(userInsertParam,User.class);
        int result =userService.insertByUserId(user);
        return result>0?Result.success():Result.failure();
    }
    @RequestMapping("/update-by-user-id")
    public Result update(@RequestBody UserUpdateParam userUpdateParam){
        User user=BeanUtil.copyProperties(userUpdateParam,User.class);
        int result=userService.updateByUserId(user);
        return result>0?Result.success():Result.failure();
    }
    @RequestMapping("/list")
    public Result list(){
        List<User> users=userService.selectAllUser();
        if (ObjectUtil.isEmpty(users)) {
            return Result.failure();
        }
        List<UserVo> userVos = BeanUtil.copyToList(users, UserVo.class);
        return Result.success(userVos);
    }
    @RequestMapping("/delete-by-user-id/{user-id}")
    public Result deleteByUserId(@PathVariable("user-id") Integer userId) {
        int result = userService.deleteByUserId(userId);
        return result > 0 ? Result.success() : Result.failure();
    }
    @RequestMapping("/delete-by-user-ids")
    public Result deleteByBannerIds(@RequestParam("user-ids") Integer[] userIds) {
        int result = userService.deleteByUserIds(List.of(userIds));
        return result > 0 ? Result.success() : Result.failure();
    }
    @RequestMapping("/page")
    public Result page(@RequestParam("page") Integer page,
                       @RequestParam("size") Integer size) {
        PageInfo<User> result =userService.selectBypage(page, size);
        if (ObjectUtil.isEmpty(result)) {
            return Result.failure();
        }
       UserVoPage userVoPage = new UserVoPage();
        userVoPage.setPage(result.getPageNum());
        userVoPage.setSize(result.getPageSize());
        userVoPage.setTotal(result.getTotal());
        userVoPage.setPages(result.getPages());
        userVoPage.setPageNums(result.getNavigatepageNums());
        userVoPage.setUserVos(BeanUtil.copyToList(result.getList(), UserVo.class));
        return Result.success(userVoPage);
    }
    @RequestMapping("/login")
    public Result page(@RequestParam String username,
                       @RequestParam String password) {
        User user=userService.login(username,password);
        return user!=null?Result.success(BeanUtil.copyProperties(user,UserVo.class)):null;
    }
    @RequestMapping("/today-count")
    public Result todayCount() {
        return Result.success(userService.todayCount());
    }
    @RequestMapping("/total-count")
    public Result totalCount() {
        return Result.success(userService.totalCount());
    }
    @RequestMapping("/gender-count")
    public Result genderCount() {
        return Result.success(userService.genderCount());
    }
    @RequestMapping("/growth-count")
    public Result growthCount() {
        return Result.success(userService.growthCount());
    }
    @RequestMapping("/upload-avatar")
    public Result uploadBanner(@RequestParam("avatar-file") MultipartFile avatarFile,
                               @RequestParam("user-id") Integer userId) {
        int result = userService.uploadAvatar(avatarFile, userId);
        return result > 0 ? Result.success() : Result.failure();
    }
}
